//
//  LoginViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/30.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "LoginViewController.h"
#import "MainTabbarController.h"
#import "RegisterViewController.h"
#import "ForgetViewController.h"
#import "KF5SDK.h"
#import "SAFNetworking.h"
#import "LoginStore.h"


@interface LoginViewController ()
{
    
    __weak IBOutlet UITextField *accountTf;
    __weak IBOutlet UITextField *passTf;
}
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topHeight;

@property (nonatomic, strong) UserDefaultsManager *userManager;

@end

@implementation LoginViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    //禁用滑动手势
    self.fd_interactivePopDisabled = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    NSUserDefaults *standDefault = [NSUserDefaults standardUserDefaults];
    accountTf.text = [standDefault objectForKey:@"username"];
    accountTf.keyboardType = UIKeyboardTypeNumberPad;
//    accountTf.clearButtonMode = 
    _topHeight.constant = iPhoneX_Top + 50;
    
    self.userManager = [UserDefaultsManager sharedInstance];
    
    self.view.backgroundColor = [UIColor whiteColor];
    ViewHeader *header = [[ViewHeader alloc]initWithString:@"登录"];
    [self.view addSubview:header];
    
}

#pragma mark - 登录
- (IBAction)onLoginBtn:(id)sender {
    
    [self.view endEditing:YES];
    
    if (accountTf.text.length <= 0) {
        
        return [self showMBPError:@"请输入账号"];
    }
    
    if (passTf.text.length <= 0) {
        
        return [self showMBPError:@"请输入密码"];
    }
    [self.view makeToastActivity:CSToastPositionCenter];
    
    WS(weakSelf);
    [[[LoginStore alloc]init] PostWithCustomName:[accountTf.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] passWord:[passTf.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] Success:^(UserDefaultsManager * _Nonnull model) {
        [weakSelf.view hideToastActivity];
        SAFNetworking *manager = [SAFNetworking shareInstance];
        [manager.requestSerializer setValue:[[NSUserDefaults standardUserDefaults]objectForKey:@"token"] forHTTPHeaderField:@"Authorization"];
        [self showMBPError:@"登录成功"];
        [self UpDateWithYiC];
        [self updateToken];
        [self performSelector:@selector(setWindow) withObject:nil afterDelay:1.0f];
    } Failure:^(NSError * _Nonnull error) {
        [weakSelf.view hideToastActivity];
        [self showMBPError:[HttpTool handleError:error]];
    }];
}

//更新或者初始化逸创云
- (void)UpDateWithYiC{
    KFUser *user = [KFUserManager shareUserManager].user;
    user = [KFUser userWithDict:@{@"userToken":self.userManager.deviceToken,@"phone":self.userManager.user_name,@"userName":self.userManager.real_name}];
    [[KFUserManager shareUserManager] setUser:user];
    [[KFUserManager shareUserManager] initializeWithParams:@{KF5Email:[NSString stringWithFormat:@"%@@forcard.com",self.userManager.user_id],KF5Phone:self.userManager.user_name,KF5Name:self.userManager.real_name} completion:^(KFUser * _Nullable user, NSError * _Nullable error) {
        NSLog(@"%@",error);
    }];
}
- (void)updateToken{
    [[[LoginStore alloc]init] updateWithDeviceToken];
}
- (void)setWindow{
    UIWindow *window = [AppDelegate sharedAppDelegate].window;
    MainTabbarController *TAB = [[MainTabbarController alloc]init];
    window.rootViewController = TAB;
}
#pragma mark - 注册
- (IBAction)onRegistBtn:(UIButton *)sender {
    
    [[AppDelegate sharedAppDelegate] pushViewController:[[RegisterViewController alloc] init]];
    
}

#pragma mark - 忘记密码
- (IBAction)onForgotBtn:(UIButton *)sender {
    
    [[AppDelegate sharedAppDelegate] pushViewController:[[ForgetViewController alloc]init]];
    
}
@end
